# -*- coding: utf-8 -*-
#*******************************************************************************
#   
#   http://mh.fumanhua.net/manhua/\d+/ 
#   http://www.fumanhua.net/manhua/\d+/
#
#*******************************************************************************
from base import *

tag='fumanhua'

#---------------------------- 验证模块 ----------------------------

# 每个解析模块都含有这个函数，用来判断链接是否符合该模块
def url_judgement(url):
    if re.match(r'http://www.fumanhua.net/manhua/\d+/',url):
        return valid_check(url)
    elif re.match(r'http://mh.fumanhua.net/manhua/\d+/',url):
        return valid_check(url)
    return False

# 根据内容检测是否可解析
def valid_check(url):
    print('{} catch {}'.format(tag,url))
    content=request_for(url,common_headers);
    if content:
        content=content.decode('gb2312','ignore')
        if content and re.compile(r'<h1>(.*?)</h1>',re.S|re.M).findall(content)[0]=='':
            return False
        return get_loader(url)
    return False

# 下载器获取
def get_loader(url):
    return FumanhuaLoader(url,FumanhuaPageParser,FumanhuaVolParser)

#---------------------------- 下载器 ----------------------------

class FumanhuaLoader(ComicLoader):
    def __init__(self,url,comic_page_parser,comic_vol_parser):
        super().__init__(url,comic_page_parser,comic_vol_parser)

#---------------------------- 解析器 ----------------------------

class FumanhuaPageParser(ComicPageParser):
    def __init__(self,comic):
        super().__init__(comic)
        self.cover_prefix,self.vol_prefix='',''
    def work(self,content):
        if 'www.' in self.comic_url:
            self.cover_prefix,self.vol_prefix='','http://www.fumanhua.net'
        else:
            self.cover_prefix,self.vol_prefix='http://mh.fumanhua.net','http://mh.fumanhua.net'
        # title_by_time
        self.comic.title=re.compile(r'<h1>(.*?)</h1>',re.S|re.M).findall(content)[0]
        # description
        descriptions=re.compile(r'<p>(.*?)</p>',re.S|re.M).findall(content)
        des=[]
        for d in descriptions:
            des.append(d.strip())
        self.comic.description=''.join(des)
        # cover
        cover_content=re.compile(r'<div class="info_cover">(.*?)</div>',re.S|re.M).findall(content)[0]
        self.comic.cover=re.compile(r'src="(.*?)"',re.S|re.M).findall(cover_content)[0]
        # vols
        vols_content=re.compile(r'<div class="plist pnormal" id="play_0">(.*?)</div>',re.S|re.M).findall(content)
        self.feed(vols_content)
    def handle_starttag(self,tag,attrs):
        if tag=='a':
            infos=info_collect(attrs)
            self.comic.vols.append(InfoVol(self.vol_prefix+infos['href'],infos['title']))

class FumanhuaVolParser(ComicVolParser):
    def __init__(self):
        super().__init__()
        self.site_prefix='http://www.fumanhua.net/manhua/'
        self.mid=''
    def work(self,content):
        pics=re.compile(r'var qTcms_S_m_murl_e=(.*?)";',re.S|re.M).findall(content)
        if len(pics)>0:
            pics=pics[0].replace('"','')
            pics=base64.b64decode(pics).decode('UTF-8').split('$qingtiandy$')
            for pic in pics:
                self.pic_urls.append(pic)
        else:
            self.mid=re.compile(r'var m_id="(.*?)"',re.S|re.M).findall(content)[0]
            urls_content=re.compile(r"html\";'>(.*?)</select>",re.S|re.M).findall(content)[0]
            self.feed(urls_content)
            self.comic.vols.reverse()
    def handle_starttag(self,tag,attrs):
        if tag=='option':
            infos=info_collect(attrs)
            content=request_for(self.site_prefix+'/'+self.mid+'/'+infos['value']+'.html',common_headers).decode('gb2312','ignore')
            self.pic_urls.append('http://pic.pgsyq.com'+re.compile(r'var imgurl = \'(.*?)\'').findall(content)[0])